name: Automated tests

on:
  workflow_dispatch:
    inputs:
      deployment_target:
        description: Choose target
        required: true
        default: all
        type: choice
        options:
          - smoke
          - functional
          - all

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
#  download-history:
#    # if: false
#    runs-on: ubuntu-latest
#    name: Download history
#    steps:
#      - name: setup Python
#        uses: actions/setup-python@v4
#        with:
#          python-version: "3.10"
#      - run: >
#          ARTIFACT_ID=$(curl -L
#          -H "Accept: application/vnd.github+json"
#          -H "X-GitHub-Api-Version: 2022-11-28"
#          "https://api.github.com/repos/valeriymenshikov/rest_api_tests/actions/artifacts?name=allure-results"
#          | python -c "import sys, json; print(json.load(sys.stdin)['artifacts'][0]['id']);")
#          &&
#          curl -L
#          -H "Accept: application/vnd.github+json"
#          -H "Authorization: Bearer ${{ secrets.TOKEN }}"
#          -H "X-GitHub-Api-Version: 2022-11-28"
#          https://api.github.com/repos/valeriymenshikov/rest_api_tests/actions/artifacts/$ARTIFACT_ID/zip
#          -o artifacts.zip &&
#          REPORT_ID=$(curl -L
#          -H "Accept: application/vnd.github+json"
#          -H "X-GitHub-Api-Version: 2022-11-28"
#          "https://api.github.com/repos/valeriymenshikov/rest_api_tests/actions/artifacts?name=github-pages"
#          | python -c "import sys, json; print(json.load(sys.stdin)['artifacts'][0]['id']);")
#          &&
#          curl -L
#          -H "Accept: application/vnd.github+json"
#          -H "Authorization: Bearer ${{ secrets.TOKEN }}"
#          -H "X-GitHub-Api-Version: 2022-11-28"
#          https://api.github.com/repos/valeriymenshikov/rest_api_tests/actions/artifacts/$REPORT_ID/zip
#          -o pages.zip
#      - run: mkdir allure-results && unzip artifacts.zip -d allure-results
#      - run: mkdir old_pages pages_history && unzip pages.zip -d old_pages && tar -xvf old_pages/artifact.tar -C pages_history
#      - run: cp pages_history/history/* allure-results && cp -r pages_history/history allure-results
#      - name: Store allure history
#        uses: actions/upload-artifact@v3
#        with:
#          name: allure-results
#          path:
#            allure-results
#          retention-days: 1

  test:
    runs-on: ubuntu-latest
#    needs: download-history
    name: Python tests

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: setup Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.10"
      - name: install dependencies
        run: pip install -r requirements.txt
      - name: Download all workflow run artifacts
        uses: actions/download-artifact@v3
      - name: all
        if: "github.event.inputs.deployment_target == 'all'"
        run: pytest --alluredir=allure-results
        continue-on-error: true
      - name: smoke
        if: "github.event.inputs.deployment_target == 'smoke'"
        run: pytest --alluredir=allure-results -m smoke
        continue-on-error: true
      - name: functional
        if: "github.event.inputs.deployment_target == 'smoke'"
        run: pytest --alluredir=allure-results -m smoke
        continue-on-error: true
      - name: Store allure results
        uses: actions/upload-artifact@v3
        with:
          name: allure-results
          path:
            allure-results
          retention-days: 1

  generate-report:
    runs-on: ubuntu-latest
    needs: test
    name: Generate report
    steps:
      - uses: actions/setup-java@v3
        with:
          distribution: 'microsoft' # See 'Supported distributions' for available options
          java-version: '17'
      - run: sudo wget https://github.com/allure-framework/allure2/releases/download/2.23.1/allure-2.23.1.tgz && sudo tar -zxvf allure-2.23.1.tgz -C /opt/ && sudo ln -s /opt/allure-2.23.1/bin/allure /usr/bin/allure
      - name: Download all workflow run artifacts
        uses: actions/download-artifact@v3
      - run: allure generate -c allure-results -o _site
      - name: Store generated report
        uses: actions/upload-artifact@v3
        with:
          name: _site
          path:
            _site
          retention-days: 1

  publish-report:
    runs-on: ubuntu-latest
    needs: generate-report
    name: Report publication
    steps:
      - name: Download all workflow run artifacts
        uses: actions/download-artifact@v3
      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v2
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1.2.9
      - run: echo https://valeriymenshikov.github.io/rest_api_tests/